package com.cloudeasy.user.controller.admin;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.cloudeasy.api.util.ResultUtil;
import com.cloudeasy.api.vo.PageResult;
import com.cloudeasy.api.vo.Result;
import com.cloudeasy.api.vo.user.PermissionVO;
import com.cloudeasy.user.po.Permission;
import com.cloudeasy.user.service.PermissionService;
import com.cloudeasy.user.util.IPageUtil;
import com.cloudeasy.user.util.PermissionUtil;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 权限由系统功能所决定，因此不提供增加、修改、删除功能
 */
@RestController
@CrossOrigin
@RequestMapping("/permission")
public class PermissionController {

    private final PermissionService permissionService;

    public PermissionController(PermissionService permissionService) {
        this.permissionService = permissionService;
    }

    /**
     * 获取所有权限
     *
     * @return {@link List<PermissionVO>}的{@link Result}
     */
    @GetMapping
    public Result<List<PermissionVO>> list() {
        return ResultUtil.success(PermissionUtil.convert(permissionService.list()));
    }

    /**
     * 分页获取所有权限
     *
     * @param page 页数
     * @param size 每页大小
     * @return {@link PageResult<PermissionVO>}的{@link Result}
     */
    @GetMapping("/{page}/{size}")
    public Result<PageResult<PermissionVO>> page(@PathVariable("page") Integer page, @PathVariable("size") Integer size) {
        IPage<Permission> permissionTempIPage = IPageUtil.compose(page, size);
        return ResultUtil.success(PermissionUtil.convert(permissionService.page(permissionTempIPage)));
    }
}
